﻿using System;
using System.Data;
using System.Data.SqlClient;
using DBModule;

namespace DataLogical.TableClass
{
    #region tblRole
    /// <summary>
    /// This object represents the properties and methods of a tblRole.
    /// </summary>

    public class tblRole
    {
        protected int _iD;
        protected string _rOLE_NAME = String.Empty;

        public tblRole()
        {
        }

        public tblRole(int ID)
        {
            SqlModule db = CommonVar.s_SqlModule;
            string strSql = string.Format("SELECT ID,ROLE_NAME FROM tblRole WHERE ID = {0}", ID);
            SqlDataReader reader = db.FastQueryBySql(strSql);
            if (reader.Read())
            {
                this.LoadFromReader(reader);
                reader.Close();
            }
            else
            {
                if (!reader.IsClosed) reader.Close();
                throw new ApplicationException("tblRole does not exist.");
            }
        }

        public tblRole(SqlDataReader reader)
        {
            this.LoadFromReader(reader);
        }

        protected void LoadFromReader(SqlDataReader reader)
        {
            if (reader != null && !reader.IsClosed)
            {
                _iD = reader.GetInt32(0);
                if (!reader.IsDBNull(1)) _rOLE_NAME = reader.GetString(1);
            }
        }

        public void Delete(int ID)
        {
            SqlModule db = CommonVar.s_SqlModule;
            string strSql = string.Format("Delete From tblRole Where ID={0}", ID);
            db.ExecSql(strSql);
        }

        public DataTable GetAll()
        {
            string strSql = "SELECT ID,ROLE_NAME FROM tblRole";
            SqlModule db = CommonVar.s_SqlModule;
            return db.QueryBySql(strSql);
        }

        public int Insert(tblRole obj)
        {
            int rtn = 0;
            string strSql = "Insert Into tblRole(ROLE_NAME)Values(@ROLE_NAME);SELECT @@IDENTITY";
            SqlModule db = CommonVar.s_SqlModule;
            SqlModule.ParameterCollection Params = CreateParams(obj);
            DataTable dt = db.QueryBySql(strSql, Params);
            if (dt.Rows.Count == 1)
            {
                rtn = int.Parse(dt.Rows[0][0] + "");
            }
            return rtn;
        }

        public void Update(tblRole obj)
        {
            string strSql = string.Format("Update tblRole Set ROLE_NAME=@ROLE_NAME Where ID={0}", obj.ID);
            SqlModule db = CommonVar.s_SqlModule;
            SqlModule.ParameterCollection Params = CreateParams(obj);
            db.ExecSql(strSql, Params);
        }

        private SqlModule.ParameterCollection CreateParams(tblRole obj)
        {
            SqlParameter Param;
            SqlModule.ParameterCollection Result = new SqlModule.ParameterCollection();

            Param = new SqlParameter("@ROLE_NAME", SqlDbType.VarChar);
            Result.Add(Param);
            if (obj.ROLE_NAME != null)
            {
                Param.Value = obj.ROLE_NAME;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            return Result;
        }

        public static bool Exist(int ID)
        {
            SqlModule db = CommonVar.s_SqlModule;
            string strSql = string.Format("SELECT COUNT(*) From tblRole Where ID={0}", ID);
            return (db.ExecSql_Cnt(strSql) == 1);
        }

        #region Public Properties
        public int ID
        {
            get { return _iD; }
        }

        public string ROLE_NAME
        {
            get { return _rOLE_NAME; }
            set { _rOLE_NAME = value; }
        }
        #endregion
    }
    #endregion
}
